// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    //遮罩显示
    shadeshow:1,
    //模块类型展示
    type:1,/* 1，日历，2，日程 */
    //日历生成数据
    year:'',
    month:'',
    lastdayarray:[],
    dayarray:[],
    nextdayarray:[],
    firstday:'',
    endday:'',
    choosedate:'',
    //日程里面的日期组切换
    calendarloaded:0,
    left:0,
    dategrouplengths:0,
    dategroup:[]
  },
  // 事件处理函数
  changetype(e) {
    this.setData({type:e.currentTarget.dataset.type})
  },
  initcalendar:function(year,month){
						
						
    //日历模块###
    this.setData({
      lastdayarray : [],
      dayarray : [],
      nextdayarray : []
    })
    
    
    //获取今天日期月份以及哪天
    var thismonth = new Date().getMonth()+1 < 10 ? '0'+(new Date().getMonth()+1) : new Date().getMonth()+1
    var thisdate = new Date().getDate() < 10 ? '0'+(new Date().getDate()) : new Date().getDate()
    
    
    
    //获取年月
    if(!year||!month){
      this.setData({
        year : new Date().getFullYear(),
        month : new Date().getMonth()+1 < 10 ? ( '0'+(new Date().getMonth()+1) ) : new Date().getMonth()+1						
      })					
    }else{
      this.setData({
        year : year,
        month : month < 10 ? ( '0'+month ) : month
      })
    }
    //分析平闰年，true是29天
    var flag = this.data.year % 400 == 0 || (this.data.year % 4 == 0 && this.data.year % 100 != 0);
    
    
    
    //当月的第一天
    this.setData({
      firstday : new Date(this.data.year+'-'+this.data.month+'-01').getDay()
    })
    //每月天数封装
    function allday(month){
      if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){return 31};if(month == 2){return flag?29:28};if(month == 4 || month == 6 || month == 9 || month == 11 ){return 30};
    }
    //当月的结束那天
    var enddate = allday(Number(this.data.month))
    this.setData({endday : new Date(this.data.year+'-'+this.data.month+'-'+enddate).getDay()})
    
    
    
    
    //前面的可见天数
    var lastdayarray = []
    for(var i=0;i<this.data.firstday;i++){
      var lastmonth = Number(this.data.month) - 1 < 0?12:Number(this.data.month) - 1
      var lastyear = lastmonth == 12 ? this.data.year - 1 : this.data.year
      var fulldate = lastyear+'-'+(lastmonth<10?'0'+lastmonth:lastmonth)+'-'+(allday(Number(this.data.month)-1<1?12:Number(this.data.month)-1) - i)
      var temp = {
        date:fulldate,
        day:allday(Number(this.data.month)-1<1?12:Number(this.data.month)-1) - i,
        iftoday:new Date(fulldate).getTime() == new Date(new Date().getFullYear()+'-'+thismonth+'-'+thisdate).getTime()?1:0
      };
      lastdayarray = this.data.lastdayarray
      lastdayarray.push( temp )
      this.setData({lastdayarray : lastdayarray})
    }
    lastdayarray.sort((a,b)=>{
      return a.day - b.day
    })
    this.setData({lastdayarray : lastdayarray})
    //当月天数
    for(var i=0;i<allday(Number(this.data.month));i++){
      var newday = (i+1<10?'0'+(i+1):(i+1))
      var fulldate = this.data.year+'-'+this.data.month+'-'+newday
      var temp = {
        date:fulldate,
        day:i+1,
        iftoday:new Date(fulldate).getTime() == new Date(new Date().getFullYear()+'-'+thismonth+'-'+thisdate).getTime()?1:0
      }
      var dayarray = this.data.dayarray
      dayarray.push( temp )
      this.setData({dayarray : dayarray})
    }
    //后面可见天数
    var nextdayarray = [];
    var newendday = this.data.endday==0?7:7-this.data.endday;
    if((this.data.lastdayarray.length + this.data.dayarray.length + newendday - 1) / 7 < 6){
      newendday = newendday + 7
    }
    for(var i=0;i<newendday;i++){
      var nextmonth = Number(this.data.month) + 1 > 12?1:Number(this.data.month) + 1
      var nextyear = nextmonth == 1 ? this.data.year + 1 : this.data.year
      var newday = (i+1<10?'0'+(i+1):(i+1))
      var fulldate = nextyear+'-'+(nextmonth<10?'0'+nextmonth:nextmonth)+'-'+newday
      var temp = {
        date:fulldate,
        day:i+1,
        iftoday:new Date(fulldate).getTime() == new Date(new Date().getFullYear()+'-'+thismonth+'-'+thisdate).getTime()?1:0
      }
      nextdayarray = this.data.nextdayarray
      nextdayarray.push( temp )
      this.setData({nextdayarray : nextdayarray})
    }
    nextdayarray.splice(this.data.nextdayarray.length-1,1)
    this.setData({nextdayarray : nextdayarray})
    
    
    
    //日历模块点击日期
    this.choosedatefn = (e) => {
      this.setData({choosedate : e.currentTarget.dataset.type})
      if(this.data.choosedate == this.data.year + '-' + this.data.month + '-' + this.data.date){
        this.setData({choosedate : ''})
      }
    }
    
    
    //左右翻页方法
    this.checkMonth = function(i) {
      if (i<10){
        i="0" + i;
      }
      return i;
    }
    this.downmonth = () => {
      this.setData({shadeshow:1})
      let temp = new Date(this.data.year + '-' + this.data.month + '-01')
      let lastDate = temp.setMonth(temp.getMonth() - 1); // 输出日期格式为毫秒形式1551398400000
       
      lastDate = new Date(lastDate);
      let lastYear = lastDate.getFullYear();
      let lastMonth = this.checkMonth(lastDate.getMonth() + 1); // 因日期中的月份表示为0-11，所以要显示正确的月份，需要 + 1
      this.initcalendar(lastYear,Number(lastMonth))
      this.downmonth_cb()
    }
    this.addmonth = () => {
      this.setData({shadeshow:1})
      let temp = new Date(this.data.year + '-' + this.data.month + '-01')
      let nextDate = temp.setMonth(temp.getMonth() + 1); // 输出日期格式为毫秒形式1556668800000
       
      nextDate = new Date(nextDate);
      let nextYear = nextDate.getFullYear();
      let nextMonth = this.checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11，所以要显示正确的月份，需要 + 1
      this.initcalendar(nextYear,Number(nextMonth))
      this.addmonth_cb()
    }
    
    
    
    
    
    //获取今天
    this.gettoday = () => {
      this.setData({choosedate : ''})
      if(this.data.year != new Date().getFullYear() || this.data.year == new Date().getFullYear()&&Number(this.data.month) != new Date().getMonth()+1){
        this.setData({shadeshow : 1})
        this.resetdategroup()
        this.request_event()
        this.initcalendar(new Date().getFullYear(),new Date().getMonth()+1)
      }else{
        this.initcalendar(new Date().getFullYear(),new Date().getMonth()+1)
      }
    }
    
    
    
    
    
    //左右翻页回调(日历模块联动其他模块)
    this.downmonth_cb = () => {
      this.resetdategroup()
      this.request_event()
    }
    this.addmonth_cb = () => {
      this.resetdategroup()
      this.request_event()
    }
    
    
    
    
    //日程模块###
    //日程里面的日期分组初始化
    this.resetdategroup = () => {
      this.setData({
        left : 0,
        dategroup : []
      })
      var size = 0 //储存每组长度
      var dategrouplengths = 0 //用来记录递归次数
      var smallgroupfn = () => {
        var smallgroup = [] //储存当前组数据
        if(this.data.dayarray.length>28){
          this.setData({dategrouplengths : 5})
        }else{
          this.setData({dategrouplengths : 4})
        }
        if(dategrouplengths<this.data.dategrouplengths){
          dategrouplengths = dategrouplengths + 1
          var lastsize = size + 7
          smallgroup = this.data.dayarray.slice(size,lastsize)
          var dategroup = this.data.dategroup
          dategroup.push(smallgroup)
          this.setData({dategroup : dategroup})
          size = dategrouplengths * 7
          smallgroupfn()
        }
      }
      smallgroupfn()
    }
    
    
    
    
    //轮播切换日期组
    this.dateeventchangegroup = (e) => {
      if(e.currentTarget.dataset.type == 'l'){
        var temp  = this.data.left - 1
        if(temp < 0){
          this.setData({left : 0})
        }else {
          this.setData({left : temp})
        }
      }else{
        var temp  = this.data.left + 1
        if(temp > this.data.dategrouplengths - 1){
          this.setData({left : this.data.dategrouplengths - 1})
        }else {
          this.setData({left : temp})
        }
      }
    }
    
    
    
    
    //请求日程事件
    this.request_event = (thedata)=>{
      //...默认显示当月全部日程(设置请求前后的节流开关)
      setTimeout(()=>{
        this.setData({shadeshow : 0})
      },1000)
    }
    
    
    
    
    
    if(this.data.calendarloaded == 0){
      this.setData({calendarloaded : 1})
      this.resetdategroup()
      this.request_event()
    }
    
    /* --- */
  },
  onLoad() {
    this.initcalendar()
  }
})
